/**
 * PersonController.java
 *
 * Created on 16 Jul 2010 23:39:50
 */
package org.blueoxygen.rest.geneology.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.rest.HttpHeaders;
import org.blueoxygen.cimande.CimandeAction;
import org.blueoxygen.rest.geneology.Person;
import org.blueoxygen.rest.geneology.RelationshipStatus;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ModelDriven;

/**
 * @author Dian Aditya
 *
 */
@Action(value = "person", results = { @Result(name = CimandeAction.SUCCESS, type = "redirect", location = "/person") })
public class PersonController extends CimandeAction implements
		ModelDriven<Object> {
	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	protected Person person = new Person();
	protected List<Person> persons = new ArrayList<Person>();
	protected List<Person> childs = new ArrayList<Person>();

	public HttpHeaders index() {
		Criteria crit = session.createCriteria(Person.class);
		String skey = request.getParameter("skey");
		crit.add(Restrictions.like("firstName", "%"
				+ (skey == null ? "" : skey) + "%"));
		persons = crit.list();

		return headers.renderResult("person/person-result").disableCaching();
	}
	public HttpHeaders photo() throws IOException, SQLException {
		HttpServletResponse response = ServletActionContext.getResponse();
		OutputStream out = response.getOutputStream();
		String id = person.getId();
		Person p = (Person) session.get(Person.class, id);
		response.setContentType(p.getContentType());
		out.write(p.getPhoto().getBytes(1, (int) p.getPhoto().length()));
		return null;
	}
	public String editNew() {
		person = new Person();

		return "person/person-add";
	}

	public String delete() {
		person = (Person) session.get(Person.class, id);

		session.beginTransaction();
		session.delete(person);
		session.getTransaction().commit();

		return SUCCESS;
	}

	public String edit() {
		person = (Person) session.get(Person.class, id);

		return "person/person-edit";
	}

	public String show() {
		System.out.println(person.getId());
		person = (Person) session.get(Person.class, id);

		Person p = (Person) session.get(Person.class, id);
		String hql = "FROM " + Person.class.getName() + "";
		if (p.isMale()) {
			hql += " WHERE father.id = '" + p.getId() + "'";
		} else {
			hql += " WHERE mother.id = '" + p.getId() + "'";
		}
		childs = session.createQuery(hql).list();

		return "person/person-detail";
	}

	public String create() {
		save(true);

		return SUCCESS;
	}

	public String update() {
		save(false);

		return SUCCESS;
	}

	private void save(boolean create) {
		Person temp = new Person();

		if (create) {
			temp = person;
			temp.setId(null);
			try {
				temp.setPhoto(Hibernate.createBlob(new FileInputStream(getUserImage())));
				temp.setContentType(getUserImageContentType());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			temp = (Person) session.get(Person.class, id);
			temp.setFirstName(person.getFirstName());
			temp.setLastName(person.getLastName());
			temp.setMale(person.isMale());
			temp.setBirthDate(person.getBirthDate());
			temp.setAddress(person.getAddress());
			temp.setCity(person.getCity());
			temp.setState(person.getState());
			temp.setCountry(person.getCountry());
			temp.setMobile(person.getMobile());
			temp.setEmail(person.getEmail());
		}

		int rstatus = new Integer(request.getParameter("rstatus"));

		GregorianCalendar calendar = new GregorianCalendar();

		temp.setStatus(RelationshipStatus.values()[rstatus]);

		session.beginTransaction();
		session.save(temp);
		session.getTransaction().commit();
	}

	public List<Person> getChilds() {
		return childs;
	}

	public Object getModel() {
		return persons.size() > 0 ? persons : person;
	}

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

}
